package com.schooleducation.controller.teacher;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.schooleducation.entity.Lesson;
import com.schooleducation.entity.LessonContent;
import com.schooleducation.entity.LessonInfo;
import com.schooleducation.entity.User;
import com.schooleducation.service.ServiceFactory;

@Controller
@RequestMapping("teacher/showLesson")
public class ShowLessonInfoController {

	@Resource(name = "serviceFac")
	private ServiceFactory serviceFac;

	@RequestMapping(value = "{id:\\+?[1-9][0-9]*$}", method = RequestMethod.GET)
	public String getPage(@PathVariable(value = "id") int lessonId, HttpSession session, Model model) {
		User teacher = (User) session.getAttribute("teacher");
		if (teacher != null) {
			Lesson l = this.serviceFac.getLessonService().getLessonById(lessonId);
			LessonInfo li = this.serviceFac.getLessonService().getLessonInfoById(lessonId);
			List<LessonContent> lcs = this.serviceFac.getLessonService().getLessonContentList(lessonId);
			Long stuTotal = this.serviceFac.getUserService().getStudentCountByLesson(l);
			
			model.addAttribute("teacher", teacher);
			model.addAttribute("lesson", l);
			model.addAttribute("lessonInfo", li);
			model.addAttribute("contentList", lcs);
			model.addAttribute("stuTotal", stuTotal);
			
			return "teacher/showLessonInfo";
		} else {
			return "redirect:../../login";
		}
	}
}
